Fetching Positions
Retrieving details about positions held in liquidity pools is an essential part of managing your liquidity and monitoring performance. This guide explains how to use the SDK to gather information about all active positions held by a given wallet.
1. Overview of Fetching Positions
Fetching positions helps developers retrieve information on liquidity positions associated with a specific wallet. It scans the Solana blockchain for token accounts owned by the wallet, determines which ones represent positions, and decodes the data to provide detailed information about each position.
With this function, you can:
- Identify all liquidity positions held by a wallet.
- Gather information about liquidity, price ranges, and fees earned.
2. Getting Started Guide
Fetching Positions for a Wallet
Fetching positions is a straightforward process:
- RPC Client: Use a Solana RPC client to interact with the blockchain.
- Wallet Address: Provide the wallet address of the user whose positions you want to fetch.
- Fetch Positions: Use the appropriate function to retrieve all positions held by the specified wallet.
- Typescript
- Rust
import { fetchPositionsForOwner, setWhirlpoolsConfig } from '@orca-so/whirlpools';
import { createSolanaRpc, devnet, address } from '@solana/web3.js';
await setWhirlpoolsConfig('solanaDevnet');
const devnetRpc = createSolanaRpc(devnet('https://api.devnet.solana.com'));
const owner = address("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt"); // set an owner address
const positions = await fetchPositionsForOwner(devnetRpc, owner);
console.log(positions);
use orca_whirlpools::{
fetch_positions_for_owner, set_whirlpools_config_address, WhirlpoolsConfigInput
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
#[tokio::main]
async fn main() {
set_whirlpools_config_address(WhirlpoolsConfigInput::SolanaDevnet).unwrap();
let rpc = RpcClient::new("https://api.devnet.solana.com".to_string());
let whirlpool_address =
Pubkey::from_str("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt").unwrap();
let positions = fetch_positions_for_owner(&rpc, whirlpool_address)
.await
.unwrap();
println!("Positions: {:?}", positions);
}
Fetching Positions in a Whirlpool
Fetching all positions in a Whirlpool is a straightforward process:
- RPC Client: Use a Solana RPC client to interact with the blockchain.
- Whirlpool Address: Provide the whirlpool address for the positions you want to fetch.
- Fetch Positions: Use the appropriate function to retrieve all positions in a whirlpool.
- Typescript
- Rust
import { fetchPositionsInWhirlpool, setWhirlpoolsConfig } from '@orca-so/whirlpools';
import { createSolanaRpc, devnet, address } from '@solana/web3.js';
await setWhirlpoolsConfig('solanaDevnet');
const devnetRpc = createSolanaRpc(devnet('https://api.devnet.solana.com'));
const owner = address("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt");
const positions = await fetchPositionsInWhirlpool(devnetRpc, owner);
console.log(positions);
use orca_whirlpools::{
fetch_positions_in_whirlpool, set_whirlpools_config_address, WhirlpoolsConfigInput,
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
#[tokio::main]
async fn main() {
set_whirlpools_config_address(WhirlpoolsConfigInput::SolanaDevnet).unwrap();
let rpc = RpcClient::new("https://api.devnet.solana.com".to_string());
let whirlpool_address =
Pubkey::from_str("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt").unwrap();
let positions = fetch_positions_in_whirlpool(&rpc, whirlpool_address)
.await
.unwrap();
println!("Positions: {:?}", positions);
}
3. Usage example
Suppose you want to monitor all active positions held by a wallet. By using the SDK to fetch positions, you can retrieve detailed information about each position, including liquidity amounts, associated pools, and earned rewards. This data can also be used to build a bot that rebalances or repositions liquidity according to a strategy defined by an algorithmic trader. Monitoring position performance helps in making informed decisions about adjusting, rebalancing, or closing positions.
4. Next steps
After fetching positions, you could:
- Add or Remove Liquidity: Adjust the amount of liquidity in your position based on market conditions.
- Harvest Rewards: Collect rewards and fees without closing the position.
- Close Position: When you decide to exit, close the position and withdraw the provided tokens along with any earned fees.
By fetching the positions, you gain visibility into your liquidity positions and can take necessary actions to optimize returns.